package com.yc.jpyy.driver.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.base.util.PageBean;
import com.yc.jpyy.driver.service.DriverService;
import com.yc.jpyy.utils.Tools;

@Controller
@RequestMapping(value = "/driver")
@Scope("prototype")
public class DirverController extends BaseController {

	@Autowired
	private DriverService driverService;
	private static Logger logger = LoggerFactory.getLogger(DirverController.class);

	@SuppressWarnings("finally")
	@RequestMapping(value = "/driverAdd", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String driverAdd(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		try {
			/*InputStream is = request.getInputStream();
			byte[] bytes = new byte[1024 * 1024];
			int nRead = 1;
			int nTotalRead = 0;
			while (nRead > 0) {
				nRead = is.read(bytes, nTotalRead, bytes.length - nTotalRead);
				if (nRead > 0)
					nTotalRead = nTotalRead + nRead;
			}
			String info = new String(bytes, 0, nTotalRead, "utf-8");*/
			String info = request.getAttribute("info").toString();
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			System.out.println(paramsMap);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			driverService.addDriverServer(paramsMap);
			response.setReturnMsg(1, "操作成功");
			return response.toJSON();

		} catch (Exception e) {
			logger.error("新增司机异常" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("操作失败");
			e.printStackTrace();
		}

		finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}

	}

	@SuppressWarnings("finally")
	@RequestMapping(value = "/delDriver", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String delDriver(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		Integer id = Integer.parseInt(request.getParameter("id"));

		try {

			String info = request.getParameter("info");
			System.out.println("info" + info);
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			System.out.println(id);
			int ret = driverService.delDriverServer(paramsMap);
			if (ret == 1) {
				response.setReturnMsg(1, "删除成功");
				return response.toJSON();
			} else {
				response.setReturnMsg(0, "删除失败");
				return response.toJSON();
			}

		} catch (Exception e) {
			logger.info("删除公司异常" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("操作失败");
			response.setReturnException(e.toString());
			e.printStackTrace();
		}

		finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}

	}

	@SuppressWarnings("finally")
	@RequestMapping(value = "/findDriverByID", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findDriverByID(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			Map<String, Object> val = driverService.findDriverByIDServer(paramsMap);
			response.setReturnMsg(1, "查询单条数据成功");
			response.setListData(val);
			return response.toJSON();
		} catch (Exception e) {
			logger.error("查询单条数据异常" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("查询单条数据异常");
			response.setReturnException(e.toString());
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}

	@SuppressWarnings("finally")
	@RequestMapping(value = "/findDriverByCertif", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findDriverByCertif(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		try {
			String info = request.getParameter("info");
			System.out.println("info" + info);
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();

			HandlerDataSource.setDataSource(dbcode);
			Map<String, Object> val = new HashMap<String, Object>();
			if(driverService.findDriverByCertif(paramsMap)!=null)
		          val = driverService.findDriverByCertif(paramsMap);
			if (val.size() == 0) {
				response.setReturnMsg(1, "查询无数据");
				response.setListData(val);
				return response.toJSON();
			} else {
				response.setReturnMsg(1, "查询单条数据成功");
				response.setListData(val);
				return response.toJSON();
			}

		} catch (Exception e) {
			logger.error("查询单条数据异常" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("查询单条数据异常11xx");
			response.setReturnException(e.toString());

		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}

	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateDriver", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateDriver(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		try {
			/*InputStream is = request.getInputStream();
			byte[] bytes = new byte[1024 * 1024];
			int nRead = 1;
			int nTotalRead = 0;
			while (nRead > 0) {
				nRead = is.read(bytes, nTotalRead, bytes.length - nTotalRead);
				if (nRead > 0)
					nTotalRead = nTotalRead + nRead;
			}
			String info = new String(bytes, 0, nTotalRead, "utf-8");*/
			String info = request.getAttribute("info").toString();
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			driverService.updateDriverServer(paramsMap);
			response.setReturnMsg(1, "操作成功");
			return response.toJSON();

		} catch (Exception e) {
			logger.error("driver" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("操作失败");
			response.setReturnException(e.toString());
			e.printStackTrace();
		}

		finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}

	}

	/**
	 * 
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findDriver", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findDriver(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		request.setCharacterEncoding("UTF-8");
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			PageBean pageBean = driverService.findDriverServer(paramsMap);
			response.setReturnMsg(1, "操作成功");
			response.setTotalCount(pageBean.getAllRow());
			response.setTotalPage(pageBean.getTotalPage());
			response.setListData(pageBean.getList());
			return response.toJSON();

		} catch (Exception e) {
			logger.error("driver" + e.toString());
			response.setReturnCode(1);
			response.setReturnException(e.toString());
			response.setReturnMsg("查询失败！");
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}
	
	/**
	 * 
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findDriverNoPicture", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findDriverNoPicture(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		request.setCharacterEncoding("UTF-8");
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			PageBean pageBean = driverService.findDriverNoPicture(paramsMap);
			response.setReturnMsg(1, "操作成功");
			response.setTotalCount(pageBean.getAllRow());
			response.setTotalPage(pageBean.getTotalPage());
			response.setListData(pageBean.getList());
			return response.toJSON();
		} catch (Exception e) {
			logger.error("driver" + e.toString());
			response.setReturnCode(1);
			response.setReturnException(e.toString());
			response.setReturnMsg("查询失败！");
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}

	// 逻辑删除
	@SuppressWarnings("finally")
	@RequestMapping(value = "/delforUpdateDriver", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String delforUpdateDriver(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		try {
			String info = request.getParameter("info");
			System.out.println("info" + info);
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			System.out.println("paramsMap" + paramsMap);
			String ids = paramsMap.get("ids").toString();
			String[] idarray = ids.split(",");
			paramsMap.put("idarray", idarray);
			String dbcode = paramsMap.get("dbcode").toString();
			// 设置数据源
			HandlerDataSource.setDataSource(dbcode);
			int ret = driverService.delforUpdateDriverServer(paramsMap);
			if (ret > 0) {
				response.setReturnMsg(1, "逻辑删除成功");
				return response.toJSON();
			} else {
				response.setReturnMsg(0, "删除失败");
				return response.toJSON();
			}

		} catch (Exception e) {
			logger.error("删除司机异常" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("操作失败");
			response.setReturnException(e.toString());
			e.printStackTrace();
		}

		finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}

	}

	// 更新状态
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateDriverStatus", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateDriverStatus(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		try {
			String info = request.getParameter("info");
			System.out.println("info" + info);
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			System.out.println("paramsMap" + paramsMap);
			String ids = paramsMap.get("ids").toString();
			String[] idarray = ids.split(",");
			paramsMap.put("idarray", idarray);
			String dbcode = paramsMap.get("dbcode").toString();
			// 设置数据源
			HandlerDataSource.setDataSource(dbcode);
			int ret = driverService.updateDriverStatusServer(paramsMap);
			if (ret > 0) {
				response.setReturnMsg(1, "更新司机状态成功");
				return response.toJSON();
			} else {
				response.setReturnMsg(0, "更新司机状态失败");
				return response.toJSON();
			}

		} catch (Exception e) {
			logger.error("更新司机状态异常" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("操作失败");
			response.setReturnException(e.toString());
			e.printStackTrace();
		}

		finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}

	}

	@SuppressWarnings("finally")
	@RequestMapping(value = "/findDriverByIdNumber", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findDriverByIdNumber(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		try {
			String info = request.getParameter("info");
			System.out.println("info" + info);
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			int val = driverService.findDriverByIdNumberServer(paramsMap);
			response.setReturnMsg(1, "查询是否有此公司成功");
			response.setListData(val);
			return response.toJSON();
		} catch (Exception e) {
			logger.error("查询单条数据异常" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("查询单条数据异常");
			response.setReturnException(e.toString());

		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}

	/**
	 * 
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findFWJDKH", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findFWJDKHD(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		request.setCharacterEncoding("UTF-8");
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			List<Map<String, Object>> list = driverService.findFWJDKHDServer(paramsMap);
			response.setReturnMsg(1, "操作成功");
			response.setListData(list);
			return response.toJSON();

		} catch (Exception e) {
			logger.error("driver" + e.toString());
			response.setReturnCode(1);
			response.setReturnException(e.toString());
			response.setReturnMsg("查询失败！");
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}

	/**
	 * 
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findFWJDKHforNum", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findFWJDKHforNum(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		request.setCharacterEncoding("UTF-8");
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			List<Map<String, Object>> list = driverService.findFWJDKHDforNumServer(paramsMap);
			response.setReturnMsg(1, "操作成功");
			response.setListData(list);
			return response.toJSON();

		} catch (Exception e) {
			logger.error("driver" + e.toString());
			response.setReturnCode(1);
			response.setReturnException(e.toString());
			response.setReturnMsg("查询失败！");
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}

	/**
	 * 
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findFWJDKbyId", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findFWJDKbyId(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		request.setCharacterEncoding("UTF-8");
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			List<Map<String, Object>> list = driverService.findFWJDKbyIdServer(paramsMap);
			response.setReturnMsg(1, "操作成功");
			response.setListData(list);
			return response.toJSON();

		} catch (Exception e) {
			logger.error("driver" + e.toString());
			response.setReturnCode(1);
			response.setReturnException(e.toString());
			response.setReturnMsg("查询失败！");
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}

	/**
	 * 
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findDriverfromVehicle", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findDriverfromVehicle(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		request.setCharacterEncoding("UTF-8");
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			List<Map<String, Object>> list = driverService.findDriverfromVehicle(paramsMap);
			response.setReturnMsg(1, "操作成功");
			response.setListData(list);
			return response.toJSON();

		} catch (Exception e) {
			logger.error("driver" + e.toString());
			response.setReturnCode(1);
			response.setReturnException(e.toString());
			response.setReturnMsg("查询失败！");
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}

	/**
	 * 
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findVehiclefromDriver", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findVehiclefromDriver(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		request.setCharacterEncoding("UTF-8");
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			List<Map<String, Object>> list = driverService.findVehiclefromDriver(paramsMap);
			response.setReturnMsg(1, "操作成功");
			response.setListData(list);
			return response.toJSON();

		} catch (Exception e) {
			logger.error("driver" + e.toString());
			response.setReturnCode(1);
			response.setReturnException(e.toString());
			response.setReturnMsg("查询失败！");
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}
	
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findDriverList", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findDriverList(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		request.setCharacterEncoding("UTF-8");
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			List<Map<String, Object>> list = driverService.findDriverListService(paramsMap);
			response.setReturnMsg(1, "操作成功");
			response.setListData(list);
			return response.toJSON();

		} catch (Exception e) {
			logger.error("driver" + e.toString());
			response.setReturnCode(1);
			response.setReturnException(e.toString());
			response.setReturnMsg("查询失败！");
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}

		
		/**
		 * 投诉事件类型分布，根据类型统计每个类型的总数
		 * 
		 * @param request
		 * @return
		 * @throws UnsupportedEncodingException
		 */
		@SuppressWarnings("finally")
		@RequestMapping(value = "/findComplainantCompanyCountNum", method = { RequestMethod.GET,
				RequestMethod.POST }, produces = "application/json; charset=utf-8")
		public @ResponseBody String findComplainantCompanyCountNum(HttpServletRequest request) throws UnsupportedEncodingException {

			RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
			request.setCharacterEncoding("UTF-8");
			try {
				String info = request.getParameter("info");
				Map<String,Object> paramsMap = Tools.JsonToMap(info);
				String dbcode = paramsMap.get("dbcode").toString();
				HandlerDataSource.setDataSource(dbcode);
	            List<Map<String, Object>> list = driverService.findComplainantCompanyCountNum(paramsMap);
	            response.setReturnMsg(1, "操作成功");
	            response.setListData(list);
				return response.toJSON();
	            
			} catch (Exception e) {
				logger.error("findComplainantCompanyCountNum" + e.toString());
				response.setReturnCode(1);
				response.setReturnException(e.toString());
				response.setReturnMsg("查询失败！");
			} finally {
				HandlerDataSource.clear();
				return response.toJSON();
			}
		}
		
		/**
		 * 驾龄统计-饼图数据
		 * 
		 * @param request
		 * @return
		 * @throws UnsupportedEncodingException
		 */
		@SuppressWarnings("finally")
		@RequestMapping(value = "/findDriveAgeSum", method = { RequestMethod.GET,
				RequestMethod.POST }, produces = "application/json; charset=utf-8")
		public @ResponseBody String findDriveAgeSum(HttpServletRequest request) throws UnsupportedEncodingException {

			RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
			request.setCharacterEncoding("UTF-8");
			try {
				String info = request.getParameter("info");
				Map<String,Object> paramsMap = Tools.JsonToMap(info);
				String dbcode = paramsMap.get("dbcode").toString();
				HandlerDataSource.setDataSource(dbcode);
	            List<Map<String, Object>> list = driverService.findDriveAgeSum(paramsMap);
	            response.setReturnMsg(1, "操作成功");
	            response.setListData(list);
				return response.toJSON();
	            
			} catch (Exception e) {
				logger.error("findDriveAgeSum" + e.toString());
				response.setReturnCode(1);
				response.setReturnException(e.toString());
				response.setReturnMsg("查询失败！");
			} finally {
				HandlerDataSource.clear();
				return response.toJSON();
			}
		}
		
		/**
		 *   接口: 驾龄统计-列表数据
		 * 
		 * @param request
		 * @return
		 * @throws UnsupportedEncodingException
		 */
		@SuppressWarnings("finally")
		@RequestMapping(value = "/findDriveAgeList", method = { RequestMethod.GET,
				RequestMethod.POST }, produces = "application/json; charset=utf-8")
		public @ResponseBody String findDriveAgeList(HttpServletRequest request) throws UnsupportedEncodingException {

			RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
			request.setCharacterEncoding("UTF-8");
			try {
				String info = request.getParameter("info");
				Map<String,Object> paramsMap = Tools.JsonToMap(info);
				String dbcode = paramsMap.get("dbcode").toString();
				HandlerDataSource.setDataSource(dbcode);
	            List<Map<String, Object>> list = driverService.findDriveAgeList(paramsMap);
	            response.setReturnMsg(1, "操作成功");
	            response.setListData(list);
				return response.toJSON();
	            
			} catch (Exception e) {
				logger.error("findDriveAgeList" + e.toString());
				response.setReturnCode(1);
				response.setReturnException(e.toString());
				response.setReturnMsg("查询失败！");
			} finally {
				HandlerDataSource.clear();
				return response.toJSON();
			}
		}
	
	
	
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findDriverNameAndIdNumberAll", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findDriverNameAndIdNumberAll(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		request.setCharacterEncoding("UTF-8");
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			List<Map<String, Object>> list = driverService.findDriverNameAndIdNumberAllService(paramsMap);
			response.setReturnMsg(1, "操作成功");
			response.setListData(list);
			return response.toJSON();

		} catch (Exception e) {
			logger.error("findDriverNameAndIdNumberAll" + e.toString());
			response.setReturnCode(1);
			response.setReturnException(e.toString());
			response.setReturnMsg("查询失败！");
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}
	
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findDriverPunishmentforWeixin", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findDriverPunishmentforWeixin(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		request.setCharacterEncoding("UTF-8");
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			//String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource("A1");
			PageBean pageBean = driverService.findDriverPunishmentforWeixinService(paramsMap);
			response.setReturnMsg(1, "操作成功");
			response.setTotalCount(pageBean.getAllRow());
			response.setTotalPage(pageBean.getTotalPage());
			response.setListData(pageBean.getList());
			return response.toJSON();
		} catch (Exception e) {
			logger.error("driver" + e.toString());
			response.setReturnCode(1);
			response.setReturnException(e.toString());
			response.setReturnMsg("查询失败！");
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}
	
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findDriverComplaintInfoforWeixin", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findDriverComplaintInfoforWeixin(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		request.setCharacterEncoding("UTF-8");
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			HandlerDataSource.setDataSource("A1");
			PageBean pageBean = driverService.findDriverComplaintInfoforWeixinService(paramsMap);
			response.setReturnMsg(1, "操作成功");
			response.setTotalCount(pageBean.getAllRow());
			response.setTotalPage(pageBean.getTotalPage());
			response.setListData(pageBean.getList());
			return response.toJSON();

		} catch (Exception e) {
			logger.error("driver" + e.toString());
			response.setReturnCode(1);
			response.setReturnException(e.toString());
			response.setReturnMsg("查询失败！");
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}
	
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findDriverNameToPunishCountAndComplainCount", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findDriverNameToPunishCountAndComplainCount(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		try {
			String info = request.getParameter("info");
			System.out.println("info" + info);
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			List<Map<String, Object>> val = driverService.findDriverNameToPunishCountAndComplainCountService(paramsMap);
			response.setReturnMsg(1, "查询数据成功");
			response.setListData(val);
			return response.toJSON();
		} catch (Exception e) {
			logger.error("查询数据异常" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("查询数据异常");
			response.setReturnException(e.toString());
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}
	
	/**
	 * 获取司机详情
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findDriverDetailByIdWx", method = {
			RequestMethod.GET, RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findDriverDetailByIdWx(
			HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String, Object> resultMap = new HashMap<>();
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = driverService.findDriverDetailByIdWx(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("findDriverDetailByIdWx" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}

	}
	
	
	/**
	 * 司机查询统计
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/selectDriverStatistic", method = {
			RequestMethod.GET, RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String selectDriverStatistic(
			HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String, Object> resultMap = new HashMap<>();
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = driverService.selectDriverStatistic(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("selectDriverStatistic" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}

	}
	
	
	
	/**
	 * 获取离线司机信息接口
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/GetNotOnlineDriver", method = {
			RequestMethod.GET, RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String getNotOnlineDriver(
			HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String, Object> resultMap = new HashMap<>();
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = driverService.getNotOnlineDriver(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("getNotOnlineDriver" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}

	}
}
